﻿using System;
using System.Collections.Generic;
using System.Text;

namespace YJKRevitBase.StringUtility
{
	public static class StringUtilityMethod
	{
		public static List<string> SplitStringByCount(string strInput, uint useCount)
		{
			List<string> list = new List<string>();
			if (string.IsNullOrWhiteSpace(strInput))
			{
				return list;
			}
			int num;
			for (int i = 0; i < strInput.Length; i += num)
			{
                num = System.Math.Min(strInput.Length - i, (int)useCount);
				if (num == 0)
				{
					num = 1;
				}
				string item = strInput.Substring(i, num);
				list.Add(item);
			}
			return list;
		}

		public static string MergeListString(List<string> lstInput)
		{
			StringBuilder stringBuilder = new StringBuilder();
			foreach (string text in lstInput)
			{
				if (text != null)
				{
					stringBuilder.Append(text);
				}
			}
			return stringBuilder.ToString();
		}

		public static string GetNowDateTimeString()
		{
			DateTime now = DateTime.Now;
			StringBuilder stringBuilder = new StringBuilder();
			stringBuilder.Append(now.Year);
			stringBuilder.Append(now.Month);
			stringBuilder.Append(now.Day);
			stringBuilder.Append(now.Hour);
			stringBuilder.Append(now.Minute);
			stringBuilder.Append(now.Second);
			stringBuilder.Append(now.Millisecond);
			return stringBuilder.ToString();
		}

		public static bool IsPositiveInteger(string _string)
		{
			if (string.IsNullOrEmpty(_string))
			{
				return false;
			}
			for (int i = 0; i < _string.Length; i++)
			{
				if (!char.IsDigit(_string[i]))
				{
					return false;
				}
			}
			return true;
		}

		public static bool IsPositiveRealNum(string _string)
		{
			if (string.IsNullOrEmpty(_string))
			{
				return false;
			}
			foreach (char c in _string)
			{
				if (!char.IsDigit(c) && c != '.')
				{
					return false;
				}
			}
			return true;
		}

		public static string RemoveUnit(string inputStr)
		{
			bool flag = true;
			string text = string.Empty;
			for (int i = 0; i < inputStr.Length; i++)
			{
				if (inputStr[i] == '.' && flag)
				{
					flag = false;
				}
				else if (!char.IsDigit(inputStr[i]))
				{
					if (i == 1 && !flag)
					{
						text = string.Empty;
						break;
					}
					text = inputStr.Substring(0, i);
					break;
				}
			}
			if (text.Length < 1 || text == null)
			{
				return inputStr;
			}
			return text;
		}

		public static string RemoveZero(string inputStr)
		{
			string result = inputStr;
			try
			{
				result = Convert.ToDouble(inputStr).ToString();
			}
			catch (Exception)
			{
			}
			return result;
		}

		public static string CutOffEfficacious(string inputStr, int i)
		{
			if (i < 0)
			{
				return inputStr;
			}
			string result = inputStr;
			try
			{
                result = System.Math.Round(double.Parse(inputStr), i).ToString();
			}
			catch (Exception)
			{
			}
			return result;
		}

		public static bool IsAlphabet(char key)
		{
			bool result = true;
			if (char.IsDigit(key))
			{
				result = false;
			}
			else if ('+' == key || '-' == key || '.' == key || '\b' == key || '\r' == key)
			{
				result = false;
			}
			return result;
		}

		public static bool IsNumber(char key)
		{
			bool result = false;
			if (char.IsDigit(key))
			{
				result = true;
			}
			else if ('.' == key || '\b' == key)
			{
				result = true;
			}
			return result;
		}

		public static string RemoveSpace(string inputStr)
		{
			string text = string.Empty;
			for (int i = 0; i < inputStr.Length; i++)
			{
				if (!char.IsWhiteSpace(inputStr[i]))
				{
					text += inputStr[i].ToString();
				}
			}
			return text;
		}

		public static int GetStringByteLength(string inputStr)
		{
			return Encoding.Default.GetByteCount(inputStr);
		}
	}
}
